package com.wei.duowan.bbs.ui.adapter;

import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.wei.duowan.bbs.R;
import com.wei.duowan.bbs.model.BbsBean;
import com.wei.duowan.bbs.ui.activity.BaseActivity;
import com.wei.duowan.bbs.utils.DrawableUtils;
import com.wei.duowan.bbs.utils.TimeUtils;

/**
 * 作者：赵若位
 * 时间：2019/10/23 10:58
 * 联系方式：1070138445
 * 功能：论坛列表页面适配器
 */
public class ForumsIndexAdapter extends BaseRecyclerViewAdapter<BbsBean.VariablesBean.ForumThreadlistBean>
{
    private static final String TAG = ForumsIndexAdapter.class.getSimpleName();

    private static final int ADVERTISING = 0x102;//广告栏


    public ForumsIndexAdapter(AppCompatActivity activity)
    {
        super(activity, R.layout.layout_forums_index);
    }


    @Override
    public int getItemViewType(int position)
    {
        if (super.getItemViewType(position) == CONTENT && mList.get(position) != null &&
                mList.get(position).getIs_ad() == 1)
        {
            return ADVERTISING;
        }
        return super.getItemViewType(position);
    }

    @NonNull
    @Override
    public BaseViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int type)
    {
        if (type == ADVERTISING)
        {
            return BaseViewHolder.getViewHolder(mActivity,
                    LayoutInflater.from(mActivity.getApplicationContext()).inflate(R.layout.layout_forums_index_advertising, parent, false));
        }
        return super.onCreateViewHolder(parent, type);
    }

    @Override
    public void onBindViewHolder(@NonNull BaseViewHolder holder, int position)
    {
        if (getItemViewType(position) == ADVERTISING)
        {
            BbsBean.VariablesBean.ForumThreadlistBean data = mList.get(position);
            holder.setText(R.id.tv_title, data.getTitle())
                    .setImageResource(R.id.img_advertising, data.getAdSrc())
                    .itemView.setOnClickListener(view -> ((BaseActivity) mActivity).alert(data.getAdLink()));
        } else
        {
            super.onBindViewHolder(holder, position);
        }
    }

    @Override
    protected void convert(int position, @Nullable BaseViewHolder holder, @Nullable BbsBean.VariablesBean.ForumThreadlistBean data)
    {
        String format = TextUtils.isEmpty(data.getType_name()) ? null : String.format(mResources.getString(R.string.type), data.getType_name());
        String title = new StringBuffer(format).append(data.getSubject()).toString();
        SpannableString span = new SpannableString(title);
        if (!TextUtils.isEmpty(format))
        {
            ForegroundColorSpan type = new ForegroundColorSpan(mResources.getColor(R.color.colorClick));
            span.setSpan(type, 0, format.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        }

        holder.setImageResource(R.id.img_user, R.mipmap.avatar_placeholder_64)
                .setText(R.id.tv_user, data.getAuthor())
                .setText(R.id.tv_time, TimeUtils.getNewsListTime(data.getDbdateline()))//时间
                .setText(R.id.tv_comment, new StringBuffer().append(data.getReplies()).toString())
                .setText(R.id.tv_read, data.getViews())
                .setText(R.id.tv_title, span)
                .setText(R.id.tv_descript, data.getSummary())
                .setVisibility(R.id.recyclerView, (data.getImagelist() != null && data.getImagelist().size() > 1) ? View.VISIBLE : View.GONE)
                .setVisibility(R.id.img_post, (data.getImagelist() != null && data.getImagelist().size() == 1) ? View.VISIBLE : View.GONE)
                .setVisibility(R.id.tv_descript, TextUtils.isEmpty(data.getSummary()) ? View.GONE : View.VISIBLE)
                .setImageResource(R.id.img_post, (data.getImagelist() != null && data.getImagelist().size() == 1) ? data.getImagelist().get(0) : null)
                .itemView.setOnClickListener(view -> Log.e(TAG, "onClick: " + data.getTitle()));

        /*设置用户标签*/
        if (!TextUtils.isEmpty(data.getGroupid()))
        {
            Drawable drawable = DrawableUtils.getDrawableRes(mResources.getString(R.string.group).concat(data.getGroupid()));
            if (drawable != null)
            {
                drawable.setBounds(0, 0,
                        mResources.getDimensionPixelOffset(R.dimen.dp_30), mResources.getDimensionPixelOffset(R.dimen.dp_12));
                TextView user = holder.getView(R.id.tv_user);
                user.setCompoundDrawables(null, null, drawable, null);
                user.setCompoundDrawablePadding(mResources.getDimensionPixelOffset(R.dimen.dp_8));
            }
        }

        if (holder.getView(R.id.recyclerView).getVisibility() == View.VISIBLE)
        {
            RecyclerView recyclerView = holder.getView(R.id.recyclerView);
            recyclerView.setLayoutManager(new GridLayoutManager(mActivity, 3));
            BaseRecyclerViewAdapter adapter = new BaseRecyclerViewAdapter<String>(mActivity, R.layout.layout_forums_index_image)
            {
                @Override
                protected void convert(int position, @Nullable BaseViewHolder holder, @Nullable String url)
                {
                    holder.setImageResource(R.id.img_post, url);
                }
            };
            adapter.bindRecyclerView(recyclerView);
            adapter.setNewData(data.getImagelist().size() < 4 ? data.getImagelist() : data.getImagelist().subList(0, 3));
        }

    }
}
